package com.jingdianjichi.interview.server.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Preconditions;
import com.jingdianjichi.interview.api.common.Result;
import com.jingdianjichi.interview.api.req.GetShareCommentReq;
import com.jingdianjichi.interview.api.req.RemoveShareCommentReq;
import com.jingdianjichi.interview.api.req.SaveShareCommentReplyReq;
import com.jingdianjichi.interview.api.vo.ShareCommentReplyVO;
import com.jingdianjichi.interview.server.entity.po.ShareCommentReply;
import com.jingdianjichi.interview.server.entity.po.ShareMoment;
import com.jingdianjichi.interview.server.sensitive.WordFilter;
import com.jingdianjichi.interview.server.service.ShareCommentReplyService;
import com.jingdianjichi.interview.server.service.ShareMessageService;
import com.jingdianjichi.interview.server.service.ShareMomentService;
import com.jingdianjichi.interview.server.utils.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * ClassName:ShareCommentController
 * Package:com.jingdianjichi.circle.server.controller
 * Description:
 *
 * @Author fst
 * @Create 2025/2/17 23:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/circle/share/comment")
public class ShareCommentController
{
    @Resource
    private ShareMomentService shareMomentService;

    @Resource
    private ShareCommentReplyService shareCommentReplyService;

    @Resource
    private WordFilter wordFilter;

    @Resource
    private ShareMessageService shareMessageService;

    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody SaveShareCommentReplyReq req)
    {
        try {

            Preconditions.checkArgument(Objects.nonNull(req),"参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getMomentId()),"鸡圈ID不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getReplyType()),"回复类型不能为空！");


            wordFilter.check(req.getContent());

            ShareMoment moment = shareMomentService.getById(req.getMomentId());

            Preconditions.checkArgument(Objects.nonNull(moment),"鸡圈不存在！");

            Boolean result = shareCommentReplyService.saveComment(req);

            if(req.getReplyType()==1)
            {
                shareMessageService.comment(LoginUtil.getLoginId(),moment.getCreatedBy(),moment.getId());
            }
            else {
                LambdaQueryWrapper<ShareCommentReply> query = Wrappers.<ShareCommentReply>lambdaQuery()
                        .eq(ShareCommentReply::getId, req.getTargetId())
                        .select(ShareCommentReply::getCreatedBy);
                ShareCommentReply reply = shareCommentReplyService.getOne(query);
                shareMessageService.reply(LoginUtil.getLoginId(), reply.getCreatedBy(), moment.getId());
            }


            if(log.isInfoEnabled())
            {
                log.info("保存评论{}", JSON.toJSONString(result));
            }
            return Result.ok(result);

        }catch (Exception e)
        {
            log.error("保存评论异常",e);
            return Result.fail();
        }
    }

    @PostMapping("/remove")
    public Result<Boolean> remove(@RequestBody RemoveShareCommentReq req)
    {
        try {

            if(log.isInfoEnabled())
            {
                log.info("删除评论入参{}",JSON.toJSONString(req));
            }

            Preconditions.checkArgument(Objects.nonNull(req),"参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getId()),"评论ID不能为空！");

            Boolean result = shareCommentReplyService.removeComment(req);
            if(log.isInfoEnabled())
            {
                log.info("删除评论{}",JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (Exception e)
        {
            log.error("删除评论异常",e);
            return Result.fail();

        }
    }

    @PostMapping(value = "/list")
    public Result<List<ShareCommentReplyVO>> list(@RequestBody GetShareCommentReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("获取鸡圈评论内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "内容ID不能为空！");
            List<ShareCommentReplyVO> result = shareCommentReplyService.listComment(req);
            if (log.isInfoEnabled()) {
                log.info("获取鸡圈评论内容{}", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取鸡圈评论内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取鸡圈评论内容异常！");
        }
    }


}
